package Database;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Asus
 */

import java.sql.*;
public class ConnectDB {
    public static Connection conn;
    public static ResultSet result = null;
    public static Statement stmt = null;

    public ConnectDB() {
        try
        {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/PPL", "root", "");
        }
        catch (SQLException e)
        {
            System.err.println(e.getMessage());
        }
    }

    public ConnectDB(String dbAddressAndPort, String dbname, String username,String password) {
        try
        {
            conn = DriverManager.getConnection("jdbc:mysql://" + dbAddressAndPort +"/"+ dbname, username, password);
        }
        catch (SQLException e)
        {
            System.err.println(e.getMessage());
        }
    }

    public static void Disconnect()
    {
        try
        {
            if (stmt != null)
                stmt.close();
            if (conn != null)
                conn.close();
        }
        catch (SQLException e)
        {
            System.err.println(e.getMessage());
        }
    }

    public static void main(String[] args) {
         ConnectDB c = new ConnectDB();
         GetProfil();
         UpdateProfil("a", "b", "c", "d", "e", "f", "g", (float) 0.3, 5);
    }

    public static void GetProfil()
    {
        try {
        stmt = conn.createStatement();
        result = stmt.executeQuery("SELECT * FROM profil_perusahaan");
        while (result.next())
        {
            String nama_perusahaan = result.getString("nama_perusahaan");
            System.out.println(nama_perusahaan);
            String alamat = result.getString("alamat");
            System.out.println(alamat);
            String telepon = result.getString("telepon");
            System.out.println(telepon);
            String email = result.getString("email");
            System.out.println(email);
            String logo = result.getString("logo");
            System.out.println(logo);
            String welcome_msg = result.getString("welcome_msg");
            System.out.println(welcome_msg);
            String kontak_tambahan = result.getString("kontak_tambahan");
            System.out.println(kontak_tambahan);
            String jml_diskon = result.getString("jml_diskon");
            System.out.println(jml_diskon);
            String syarat_diskon = result.getString("syarat_diskon");
            System.out.println(syarat_diskon);
        }
        }
        catch (SQLException e) {
            System.out.println("Error occurred " + e);
        }
    }

    public static void UpdateProfil(String nama_perusahaan, String alamat, String telepon, String email, String logo, String welcome_msg
            , String kontak_tambahan, float jml_diskon, int syarat_diskon)
    {
        try {
        stmt = conn.createStatement();
        result = stmt.executeQuery("SELECT * FROM profil_perusahaan");
        String nama_perusahaan_lama = null;
        while (result.next())
        {
            nama_perusahaan_lama = result.getString("nama_perusahaan");
            System.out.println(nama_perusahaan_lama);
        }
        stmt.executeUpdate("DELETE FROM profil_perusahaan WHERE nama_perusahaan=" + "'" + nama_perusahaan_lama + "'");
        stmt.executeUpdate("INSERT INTO profil_perusahaan (nama_perusahaan, alamat, telepon, email, logo, welcome_msg, kontak_tambahan, jml_diskon, syarat_diskon)" +
                "VALUES" + "('" + nama_perusahaan + "', '" + alamat + "', '" + telepon + "', '" + email + "', '" + logo + "', '" + welcome_msg + "', '" + kontak_tambahan + "', '" + jml_diskon + "', '" + syarat_diskon + "')");
        }
        catch (SQLException e) {
            System.out.println("Error occurred " + e);
        }

    }
}
